import {AfterViewInit, Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {DvbConfigService} from '../../../../services/api/dvb-config.service';
import {MessageService, ModalComponent, ProgressBarService, WebsocketService} from '../../../../../ng-tools';
import {Message} from '../../../../../ng-tools/src/message/message';
import {Subscription, timer} from 'rxjs';
import {DatePipe} from '@angular/common';

@Component({
  selector: 'device-manager',
  templateUrl: './device-manager.component.html',
  styleUrls: ['./device-manager.component.scss'],
  providers: [
    DatePipe,
    WebsocketService,
  ]
})
export class DeviceManagerComponent implements OnInit, AfterViewInit, OnDestroy, Message {
  @ViewChild('modal') private modal: ModalComponent;
  deviceInfo;
  subscriptions: Subscription;
  command: number;
  breadcrumb = [
    {name: '设备管理', cssIcon: 'fa fa-fw fa-cubes'},
    {name: '设备信息', cssIcon: 'fa fa-fw fa-microchip'}
  ];
  constructor(private configService: DvbConfigService,
              private message: MessageService,
              public progress: ProgressBarService) { }

  ngOnInit() {
    this.configService.get(this.configService.urls.get_device_base_info).subscribe((resp) => {
      this.deviceInfo = resp.body;
      console.log(this.deviceInfo);
      this.message.sendMessage('page load progress', this);
    });
  }
  showWarnningInfoModal(command: number) {
    this.command = command;
    this.modal.show({
      theme: 'danger',
      title: '确认操作',
      size: 'sm',
      cssIcon: 'fa fa-fw fa-warning',
      data: '执行该操作后机器将重启或关机，确定执行？',
      confirmBtn: true,
      closeBtn: true
    });
  }

  modalEvenHandler() {
    this.modal.event.subscribe({
      next: (event) => {
        if (event.name === 'confirm') {
          this.controlDevice(this.command);
        }
      }
    });
  }
  controlDevice(command: number) {
    this.configService.post(this.configService.urls.exec_shell_command, {'shell': command}).subscribe();
  }


  ngAfterViewInit(): void {
    this.modalEvenHandler();
    this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
    // this.message.sendMessage('page load progress', this, '300ms');
  }

  ngOnDestroy(): void {
  }
}
